import { clientToLocal, clientToLocalParam } from "../../misc/lan";
import { safeRemoveFromFather } from "./uiUtils";
import { MainWindow, LayerEnum } from "../../window/MainWindow";

function getTipsLayer(): egret.DisplayObjectContainer {
	return MainWindow.ins().getLayer(LayerEnum.tips);
}


let msglineId = 1;
class MsgLine extends eui.Component {
	lbMsg: eui.Label;
	// lbMsg: TextWithImage;

	imgBg: eui.Image;

	msg: string;

	// tw:egret.Tween;
	id = 0; // for debug ...

	color = 0;

	ml: MsgLines

	index: number;

	constructor(msg: string, color: number, ml: MsgLines, index: number) {
		super();
		this.ml = ml;
		this.id = msglineId++;
		this.msg = msg;
		this.color = color;
		this.skinName = 'ui_msgline';
		this.horizontalCenter = 0;
		this.index = index;
		// this.touchEnabled = false;
		// this.touchChildren = false;
	}

	oldwidth = 0;

	protected childrenCreated(): void {
		super.childrenCreated();
		this.oldwidth = this.width;
		this.createTi();
	}

	protected createTi(): void {
		if (this.color) {
			this.lbMsg.textColor = this.color;
		}
		// this.lbMsg.text = this.msg;
		this.lbMsg.text = this.msg;
		let twidth = this.lbMsg.textWidth;
		if (this.width < twidth) {
			this.width = twidth;
		} else {
			this.width = this.oldwidth;
		}
		if (this.height < this.lbMsg.height) { this.height = this.lbMsg.height; }
	}

	moving = false;

	movey = 0;

	last = 0;

	moveTo(y: number, last: number): void {
		this.movey = y;
		this.last = last;
		this.moving = true;
		const tw = egret.Tween.get(this);
		tw.to({ y }, 300)
		.call(() => {
			this.moving = false;
			egret.Tween.removeTweens(this);
			const tw2 = egret.Tween.get(this); // 否则改y没有作用！
			tw2.wait(last / 2)
			.to({ alpha: 0.2 }, last / 2)
			.call(() => {
				this.ml.onFinshed(this);
			});
		});
	}

	moveDown(h: number): void{
		if (this.moving) {
			egret.Tween.removeTweens(this);
			this.moveTo(this.movey + h, this.last);
		} else {
			this.y += h;
		}
	}
}

class MsgLines extends eui.Component {
	private static instance: MsgLines;

	public static ins(): MsgLines {
		if (!this.instance) {
			this.instance = new MsgLines();
		}
		return this.instance;
	}

	// group: eui.Group;

	constructor() {
		super();
		const layer = getTipsLayer();
		layer.addChild(this);
		this.y = 0;
		this.horizontalCenter = 0;

		this.touchEnabled = false;
		this.touchChildren = false;
		this.skinName = 'ui_msglines';
	}

	childrenCreated(): void{
		this.prevY = this.stage.stageHeight / 2;
	}

	prevY = 0;

	index = 1;

	addMsgline(msg: string, last: number, color: number): void{
		const line = new MsgLine(msg, color, this, this.index++);
		line.y = this.stage.stageHeight / 2;

		this.addChild(line);

		line.moveTo(this.prevY - line.height, last);

		this.prevY -= line.height;
	}

	onFinshed(line: MsgLine): void{
		safeRemoveFromFather(line);
		this.prevY += line.height;
		for (const c of this.$children) {
			// 并不应该所有的都movedown，大家的生命周期不一样，只有在其之上的才需要，
			if (c instanceof MsgLine) {
				if (c.index > line.index) {
					c.moveDown(line.height);
				}
			}
		}
	}

	childrenHeight(): number {
		let h = 0;
		for (const c of this.$children) {
			h += c.height;
		}
		return h;
	}
}

export function msglineRaw(msg: string, last: number, color = 0): void{
	// console.log(msg)
	MsgLines.ins().addMsgline(msg, last, color);
	// GetMsgLines().add_msgline(msg)
}

function simpleString(arg: any): string {
	try {
		if (Array.isArray(arg) || typeof arg === 'object') {
			return JSON.stringify(arg);
		}
	} catch (e) {
		if (Array.isArray(arg)) {
			return 'array';
		}
		if (typeof arg === 'object') {
			// return JSON.stringify(arg);
			return 'object';
		}
	}
	return String(arg);
}

export function msglog(...args: any[]): void{
	console.log(...args);
	const ss = [];
	for (let i = 0; i < args.length; i++) {
		ss.push(simpleString(args[i]));
	}
	const s = ss.join(' ');
	msglineRaw(s, 5000);
}
export function log(...args: any[]): void{
	msglog(...args);
}

export function msgerror(...args: any[]): void{
	const ss = [];
	for (let i = 0; i < args.length; i++) {
		ss.push(simpleString(args[i]));
	}
	const s = ss.join(' ');
	msglineRaw(s, 10000, 0xff0000);
}

export function msgline(msg: string, last = 3000, itemId = 0): void{
	msg = clientToLocal(msg);
	// console.log(msg)
	// msgItemRaw(0, last, 0, 0, msg);
	// MsgLines.ins().addMsgline(msg, last, color);
	msglineRaw(msg, last);
	// GetMsgLines().add_msgline(msg)
}

export function msglineParam(...params: any[]): void{
	const msg = clientToLocalParam(params);
	// console.log(msg)
	msglineRaw(msg, 3000);
	// GetMsgLines().add_msgline(msg)
}
